﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KM.ENT;
using KM.BUS;
using KM.Models;
using CORE;


namespace KM.Areas.Admin.Controllers
{
    public class NewsController : Controller
    {
        //
        // GET: /Admin/News/
        int pageSize = 20;
        public ActionResult DanhSach(int Id=1)
        {
            binGN(Id.ToString());
            busNews busSP = new busNews();
            List<entNews> lst = new List<entNews>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            lst = busSP.getAll(Id, ref page);
            ViewBag.page = page;
            return View(lst);
            
        }
        [HttpPost]
        public ActionResult DanhSach(FormCollection collection)
        {
            binGN(collection["LGN"]);
            busNews busSP = new busNews();
            List<entNews> lst = new List<entNews>();
            PagingInfoModel page = new PagingInfoModel()
            {
                CurrentPage = Utils.getIntQuery("page") == 0 ? 1 : Utils.getIntQuery("page"),
                recordPerPage = pageSize
            };
            if (collection["LGN"].ToString() != "")
            {
                int Id = int.Parse(collection["LGN"].ToString());
                lst = busSP.getAll(int.Parse(collection["LGN"].ToString()), ref page);
                ViewBag.me = Id;
            }
            ViewBag.page = page;
            return View(lst);
        }
        void binGN(string check)
        {
            //lay danh sach loai san pham
            busGroupNews busGN = new busGroupNews();
            List<entGroupNews> lst = new List<entGroupNews>();
            lst = busGN.getAll();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var item in lst)
            {
                items.Add(new SelectListItem()
                {
                    Value = item.Id.ToString(),
                    Text = item.Name,
                    Selected = (item.Id.ToString() == check ? true : false)
                });
            }
            ViewBag.lstGN = items;
        }
        public RedirectToRouteResult delete(int Id)
        {
            busNews busN = new busNews();
            busN.delete(Id);
            return RedirectToAction("danhsach");
        }
        public ActionResult edit(int? Id)
        {
            entNews entN = new entNews();
            busNews busN = new busNews();
            if (Id != null)
            {
                entN = busN.getInfo(Id.Value);
            }
            List<entGroupNews> lst = new List<entGroupNews>();
            busGroupNews busGN = new busGroupNews();
            lst = busGN.getAll();
            ViewBag.lstGN = lst;
            return View(entN);
        }
        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult edit(entNews entN)
        {
            busNews busN = new busNews();
            busN.createEdit(entN);
            return RedirectToAction("danhsach");
        }

    }
}
